let BaseForm = require('@/common/vue/BaseForm');
let comDriver = require('@/common/driver');

class Bean extends BaseForm{

    async buildControl() {
        await super.buildControl();

        let { createApp } = require('vue/dist/vue.esm-bundler.js');
        let ElementPlus = require('element-plus');
        let locale = require('element-plus/lib/locale/lang/zh-cn');

        let app = createApp({
            data:
                () => {
                    let data = {};
                    this.buildData(data);
                    this.param.controls.forEach(function (control, key, map) {
                        control.buildData(data);
                    }, this);
                    return data;
                },
            components: this.param.vComponent,
            methods: this.param.vMethod,
            computed: this.param.vComputed,
        });

        app.use(ElementPlus, {
            locale: locale,
            size: 'medium'
        });

        this.param.vue = app.mount(this.getTemplateElementID());

        this.buildControlAfter();
    }

    async showStyle() {
        await super.showStyle();
        require("element-plus/dist/index.css");
    }

    showError(message, duration = 3, onClose = null) {
        this.param.vue.$message({
            message: message,
            type: 'error',
            duration: duration * 1000,
            onClose: onClose,
        });
    }

    showMessage(message, duration = 3, onClose = null) {
        this.param.vue.$message({
            message: message,
            type: 'info',
            duration: duration * 1000,
            onClose: onClose,
        });
    }

    showLoading(message, bool) {
        if (this.param.vue)
            this.param._loading = this.param.vue.$loading({
                lock: true,
                text: message,
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0)'
            });
    }

    hideLoading(dir = false) {
        if (dir){
            if (this.param._loading) this.param._loading.close();
        } else {
            //直接关闭会关闭不了
            setTimeout(() => {
                if (this.param._loading) this.param._loading.close();
            }, 100);
        }
    }

}


module.exports = Bean;
